package com.dt.platform.hr.controller;

import java.util.*;
import org.apache.commons.lang3.StringUtils;
import org.github.foxnic.web.framework.web.SuperController;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import com.github.foxnic.commons.collection.CollectorUtil;
import com.github.foxnic.dao.entity.ReferCause;
import com.github.foxnic.api.swagger.InDoc;
import org.github.foxnic.web.framework.sentinel.SentinelExceptionUtil;
import com.github.foxnic.api.swagger.ApiParamSupport;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.dt.platform.proxy.hr.LearnReleaseServiceProxy;
import com.dt.platform.domain.hr.meta.LearnReleaseVOMeta;
import com.dt.platform.domain.hr.LearnRelease;
import com.dt.platform.domain.hr.LearnReleaseVO;
import com.github.foxnic.api.transter.Result;
import com.github.foxnic.dao.data.SaveMode;
import com.github.foxnic.dao.excel.ExcelWriter;
import com.github.foxnic.springboot.web.DownloadUtil;
import com.github.foxnic.dao.data.PagedList;
import java.util.Date;
import java.sql.Timestamp;
import com.github.foxnic.api.error.ErrorDesc;
import com.github.foxnic.commons.io.StreamUtil;
import java.util.Map;
import com.github.foxnic.dao.excel.ValidateResult;
import java.io.InputStream;
import com.dt.platform.domain.hr.meta.LearnReleaseMeta;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiImplicitParam;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.dt.platform.hr.service.ILearnReleaseService;
import com.github.foxnic.api.validate.annotations.NotNull;

/**
 * <p>
 * 学习下发接口控制器
 * </p>
 * @author 金杰 , maillank@qq.com
 * @since 2024-09-14 13:54:47
 */
@InDoc
@Api(tags = "学习下发")
@RestController("HrLearnReleaseController")
public class LearnReleaseController extends SuperController {

    @Autowired
    private ILearnReleaseService learnReleaseService;

    /**
     * 添加学习下发
     */
    @ApiOperation(value = "添加学习下发")
    @ApiImplicitParams({
		@ApiImplicitParam(name = LearnReleaseVOMeta.ID, value = "主键", required = true, dataTypeClass = String.class),
		@ApiImplicitParam(name = LearnReleaseVOMeta.LEARN_ID, value = "名称", required = false, dataTypeClass = String.class),
		@ApiImplicitParam(name = LearnReleaseVOMeta.RCD_TIME, value = "下发时间", required = false, dataTypeClass = Date.class),
		@ApiImplicitParam(name = LearnReleaseVOMeta.UPDATE_BY, value = "修改人ID", required = false, dataTypeClass = String.class),
		@ApiImplicitParam(name = LearnReleaseVOMeta.STATUS, value = "启用状态", required = false, dataTypeClass = String.class),
		@ApiImplicitParam(name = LearnReleaseVOMeta.SDATE, value = "开始时间", required = false, dataTypeClass = Date.class, example = ""),
		@ApiImplicitParam(name = LearnReleaseVOMeta.EDATE, value = "结束时间", required = false, dataTypeClass = Date.class, example = ""),
		@ApiImplicitParam(name = LearnReleaseVOMeta.COST, value = "学习用时", required = false, dataTypeClass = Integer.class, example = "30"),
		@ApiImplicitParam(name = LearnReleaseVOMeta.CODE, value = "编号", required = false, dataTypeClass = String.class),
		@ApiImplicitParam(name = LearnReleaseVOMeta.IF_ALL_USER, value = "是否全员", required = false, dataTypeClass = String.class),
		@ApiImplicitParam(name = LearnReleaseVOMeta.SEL_ORG, value = "选择部门", required = false, dataTypeClass = String.class),
		@ApiImplicitParam(name = LearnReleaseVOMeta.RELEASE_STATUS, value = "下发状态", required = false, dataTypeClass = String.class)
	})
    @ApiParamSupport(ignoreDBTreatyProperties = true, ignoreDefaultVoProperties = true, ignorePrimaryKey = true)
    @ApiOperationSupport(order = 1, author = "金杰 , maillank@qq.com")
    @SentinelResource(value = LearnReleaseServiceProxy.INSERT, blockHandlerClass = { SentinelExceptionUtil.class }, blockHandler = SentinelExceptionUtil.HANDLER)
    @PostMapping(LearnReleaseServiceProxy.INSERT)
    public Result insert(LearnReleaseVO learnReleaseVO) {
        Result result = learnReleaseService.insert(learnReleaseVO, false);
        return result;
    }

    /**
     * 删除学习下发
     */
    @ApiOperation(value = "删除学习下发")
    @ApiImplicitParams({
		@ApiImplicitParam(name = LearnReleaseVOMeta.ID, value = "主键", required = true, dataTypeClass = String.class)
	})
    @ApiOperationSupport(order = 2, author = "金杰 , maillank@qq.com")
    @SentinelResource(value = LearnReleaseServiceProxy.DELETE, blockHandlerClass = { SentinelExceptionUtil.class }, blockHandler = SentinelExceptionUtil.HANDLER)
    @PostMapping(LearnReleaseServiceProxy.DELETE)
    public Result deleteById(String id) {
        this.validator().asserts(id).require("缺少id值");
        if (this.validator().failure()) {
            return this.validator().getFirstResult();
        }
        // 引用校验
        ReferCause cause = learnReleaseService.hasRefers(id);
        // 判断是否可以删除
        this.validator().asserts(cause.hasRefer()).requireEqual("不允许删除当前记录：" + cause.message(), false);
        if (this.validator().failure()) {
            return this.validator().getFirstResult().messageLevel4Confirm();
        }
        Result result = learnReleaseService.deleteByIdLogical(id);
        return result;
    }

    /**
     * 下发
     */
    @ApiOperation(value = "下发")
    @ApiOperationSupport(order = 2, author = "金杰 , maillank@qq.com")
    @SentinelResource(value = LearnReleaseServiceProxy.RELEASE, blockHandlerClass = { SentinelExceptionUtil.class }, blockHandler = SentinelExceptionUtil.HANDLER)
    @PostMapping(LearnReleaseServiceProxy.RELEASE)
    public Result release(String id, String learnId) {
//        if (StringUtils.isBlank(id)) {
//            LearnRelease release = new LearnRelease();
//            release.setRcdTime(new Date());
//            release.setLearnId(learnId);
//            learnReleaseService.insert(release, false);
//        } else {
//            LearnRelease release = learnReleaseService.getById(id);
//            release.setRcdTime(new Date());
//            learnReleaseService.update(release, SaveMode.NOT_NULL_FIELDS);
//        }
        return learnReleaseService.release(id);
    }
    /**
     * 作废
     */
    @ApiOperation(value = "作废")
    @ApiOperationSupport(order = 2, author = "金杰 , maillank@qq.com")
    @SentinelResource(value = LearnReleaseServiceProxy.CANCEL, blockHandlerClass = { SentinelExceptionUtil.class }, blockHandler = SentinelExceptionUtil.HANDLER)
    @PostMapping(LearnReleaseServiceProxy.CANCEL)
    public Result cancel(String id) {
       return  learnReleaseService.cancel(id);
    }


    /**
     * 批量删除学习下发 <br>
     * 联合主键时，请自行调整实现
     */
    @ApiOperation(value = "批量删除学习下发")
    @ApiImplicitParams({
		@ApiImplicitParam(name = LearnReleaseVOMeta.IDS, value = "主键清单", required = true, dataTypeClass = List.class, example = "[1,3,4]")
	})
    @ApiOperationSupport(order = 3, author = "金杰 , maillank@qq.com")
    @SentinelResource(value = LearnReleaseServiceProxy.DELETE_BY_IDS, blockHandlerClass = { SentinelExceptionUtil.class }, blockHandler = SentinelExceptionUtil.HANDLER)
    @PostMapping(LearnReleaseServiceProxy.DELETE_BY_IDS)
    public Result deleteByIds(List<String> ids) {
        // 参数校验
        this.validator().asserts(ids).require("缺少ids参数");
        if (this.validator().failure()) {
            return this.validator().getFirstResult();
        }
        // 查询引用
        Map<String, ReferCause> causeMap = learnReleaseService.hasRefers(ids);
        // 收集可以删除的ID值
        List<String> canDeleteIds = new ArrayList<>();
        for (Map.Entry<String, ReferCause> e : causeMap.entrySet()) {
            if (!e.getValue().hasRefer()) {
                canDeleteIds.add(e.getKey());
            }
        }
        // 执行删除
        if (canDeleteIds.isEmpty()) {
            // 如果没有一行可以被删除
            return ErrorDesc.failure().message("无法删除您选中的数据行：").data(0).addErrors(CollectorUtil.collectArray(CollectorUtil.filter(causeMap.values(), (e) -> {
                return e.hasRefer();
            }), ReferCause::message, String.class)).messageLevel4Confirm();
        } else if (canDeleteIds.size() == ids.size()) {
            // 如果全部可以删除
            Result result = learnReleaseService.deleteByIdsLogical(canDeleteIds);
            return result;
        } else if (canDeleteIds.size() > 0 && canDeleteIds.size() < ids.size()) {
            // 如果部分行可以删除
            Result result = learnReleaseService.deleteByIdsLogical(canDeleteIds);
            if (result.failure()) {
                return result;
            } else {
                return ErrorDesc.success().message("已删除 " + canDeleteIds.size() + " 行，但另有 " + (ids.size() - canDeleteIds.size()) + " 行数据无法删除").data(canDeleteIds.size()).addErrors(CollectorUtil.collectArray(CollectorUtil.filter(causeMap.values(), (e) -> {
                    return e.hasRefer();
                }), ReferCause::message, String.class)).messageLevel4Confirm();
            }
        } else {
            // 理论上，这个分支不存在
            return ErrorDesc.success().message("数据删除未处理");
        }
    }

    /**
     * 更新学习下发
     */
    @ApiOperation(value = "更新学习下发")
    @ApiImplicitParams({
		@ApiImplicitParam(name = LearnReleaseVOMeta.ID, value = "主键", required = true, dataTypeClass = String.class),
		@ApiImplicitParam(name = LearnReleaseVOMeta.LEARN_ID, value = "名称", required = false, dataTypeClass = String.class),
		@ApiImplicitParam(name = LearnReleaseVOMeta.RCD_TIME, value = "下发时间", required = false, dataTypeClass = Date.class),
		@ApiImplicitParam(name = LearnReleaseVOMeta.UPDATE_BY, value = "修改人ID", required = false, dataTypeClass = String.class),
		@ApiImplicitParam(name = LearnReleaseVOMeta.STATUS, value = "启用状态", required = false, dataTypeClass = String.class),
		@ApiImplicitParam(name = LearnReleaseVOMeta.SDATE, value = "开始时间", required = false, dataTypeClass = Date.class, example = ""),
		@ApiImplicitParam(name = LearnReleaseVOMeta.EDATE, value = "结束时间", required = false, dataTypeClass = Date.class, example = ""),
		@ApiImplicitParam(name = LearnReleaseVOMeta.COST, value = "学习用时", required = false, dataTypeClass = Integer.class, example = "30"),
		@ApiImplicitParam(name = LearnReleaseVOMeta.CODE, value = "编号", required = false, dataTypeClass = String.class),
		@ApiImplicitParam(name = LearnReleaseVOMeta.IF_ALL_USER, value = "是否全员", required = false, dataTypeClass = String.class),
		@ApiImplicitParam(name = LearnReleaseVOMeta.SEL_ORG, value = "选择部门", required = false, dataTypeClass = String.class),
		@ApiImplicitParam(name = LearnReleaseVOMeta.RELEASE_STATUS, value = "下发状态", required = false, dataTypeClass = String.class)
	})
    @ApiParamSupport(ignoreDBTreatyProperties = true, ignoreDefaultVoProperties = true)
    @ApiOperationSupport(order = 4, author = "金杰 , maillank@qq.com", ignoreParameters = { LearnReleaseVOMeta.PAGE_INDEX, LearnReleaseVOMeta.PAGE_SIZE, LearnReleaseVOMeta.SEARCH_FIELD, LearnReleaseVOMeta.FUZZY_FIELD, LearnReleaseVOMeta.SEARCH_VALUE, LearnReleaseVOMeta.DIRTY_FIELDS, LearnReleaseVOMeta.SORT_FIELD, LearnReleaseVOMeta.SORT_TYPE, LearnReleaseVOMeta.DATA_ORIGIN, LearnReleaseVOMeta.QUERY_LOGIC, LearnReleaseVOMeta.REQUEST_ACTION, LearnReleaseVOMeta.IDS })
    @SentinelResource(value = LearnReleaseServiceProxy.UPDATE, blockHandlerClass = { SentinelExceptionUtil.class }, blockHandler = SentinelExceptionUtil.HANDLER)
    @PostMapping(LearnReleaseServiceProxy.UPDATE)
    public Result update(LearnReleaseVO learnReleaseVO) {
        Result result = learnReleaseService.update(learnReleaseVO, SaveMode.DIRTY_OR_NOT_NULL_FIELDS, false);
        return result;
    }

    /**
     * 保存学习下发
     */
    @ApiOperation(value = "保存学习下发")
    @ApiImplicitParams({
		@ApiImplicitParam(name = LearnReleaseVOMeta.ID, value = "主键", required = true, dataTypeClass = String.class),
		@ApiImplicitParam(name = LearnReleaseVOMeta.LEARN_ID, value = "名称", required = false, dataTypeClass = String.class),
		@ApiImplicitParam(name = LearnReleaseVOMeta.RCD_TIME, value = "下发时间", required = false, dataTypeClass = Date.class),
		@ApiImplicitParam(name = LearnReleaseVOMeta.UPDATE_BY, value = "修改人ID", required = false, dataTypeClass = String.class),
		@ApiImplicitParam(name = LearnReleaseVOMeta.STATUS, value = "启用状态", required = false, dataTypeClass = String.class),
		@ApiImplicitParam(name = LearnReleaseVOMeta.SDATE, value = "开始时间", required = false, dataTypeClass = Date.class, example = ""),
		@ApiImplicitParam(name = LearnReleaseVOMeta.EDATE, value = "结束时间", required = false, dataTypeClass = Date.class, example = ""),
		@ApiImplicitParam(name = LearnReleaseVOMeta.COST, value = "学习用时", required = false, dataTypeClass = Integer.class, example = "30"),
		@ApiImplicitParam(name = LearnReleaseVOMeta.CODE, value = "编号", required = false, dataTypeClass = String.class),
		@ApiImplicitParam(name = LearnReleaseVOMeta.IF_ALL_USER, value = "是否全员", required = false, dataTypeClass = String.class),
		@ApiImplicitParam(name = LearnReleaseVOMeta.SEL_ORG, value = "选择部门", required = false, dataTypeClass = String.class),
		@ApiImplicitParam(name = LearnReleaseVOMeta.RELEASE_STATUS, value = "下发状态", required = false, dataTypeClass = String.class)
	})
    @ApiParamSupport(ignoreDBTreatyProperties = true, ignoreDefaultVoProperties = true)
    @ApiOperationSupport(order = 5, ignoreParameters = { LearnReleaseVOMeta.PAGE_INDEX, LearnReleaseVOMeta.PAGE_SIZE, LearnReleaseVOMeta.SEARCH_FIELD, LearnReleaseVOMeta.FUZZY_FIELD, LearnReleaseVOMeta.SEARCH_VALUE, LearnReleaseVOMeta.DIRTY_FIELDS, LearnReleaseVOMeta.SORT_FIELD, LearnReleaseVOMeta.SORT_TYPE, LearnReleaseVOMeta.DATA_ORIGIN, LearnReleaseVOMeta.QUERY_LOGIC, LearnReleaseVOMeta.REQUEST_ACTION, LearnReleaseVOMeta.IDS })
    @SentinelResource(value = LearnReleaseServiceProxy.SAVE, blockHandlerClass = { SentinelExceptionUtil.class }, blockHandler = SentinelExceptionUtil.HANDLER)
    @PostMapping(LearnReleaseServiceProxy.SAVE)
    public Result save(LearnReleaseVO learnReleaseVO) {
        Result result = learnReleaseService.save(learnReleaseVO, SaveMode.DIRTY_OR_NOT_NULL_FIELDS, false);
        return result;
    }

    /**
     * 获取学习下发
     */
    @ApiOperation(value = "获取学习下发")
    @ApiImplicitParams({
		@ApiImplicitParam(name = LearnReleaseVOMeta.ID, value = "主键", required = true, dataTypeClass = String.class, example = "1")
	})
    @ApiOperationSupport(order = 6, author = "金杰 , maillank@qq.com")
    @SentinelResource(value = LearnReleaseServiceProxy.GET_BY_ID, blockHandlerClass = { SentinelExceptionUtil.class }, blockHandler = SentinelExceptionUtil.HANDLER)
    @PostMapping(LearnReleaseServiceProxy.GET_BY_ID)
    public Result<LearnRelease> getById(String id) {
        Result<LearnRelease> result = new Result<>();
        LearnRelease learnRelease = learnReleaseService.getById(id);
        learnReleaseService.dao().fill(learnRelease).with(LearnReleaseMeta.SELECT_USER).with(LearnReleaseMeta.SELECT_ORG).execute();
        result.success(true).data(learnRelease);
        return result;
    }

    /**
     * 批量获取学习下发 <br>
     * 联合主键时，请自行调整实现
     */
    @ApiOperation(value = "批量获取学习下发")
    @ApiImplicitParams({
		@ApiImplicitParam(name = LearnReleaseVOMeta.IDS, value = "主键清单", required = true, dataTypeClass = List.class, example = "[1,3,4]")
	})
    @ApiOperationSupport(order = 3, author = "金杰 , maillank@qq.com")
    @SentinelResource(value = LearnReleaseServiceProxy.GET_BY_IDS, blockHandlerClass = { SentinelExceptionUtil.class }, blockHandler = SentinelExceptionUtil.HANDLER)
    @PostMapping(LearnReleaseServiceProxy.GET_BY_IDS)
    public Result<List<LearnRelease>> getByIds(List<String> ids) {
        Result<List<LearnRelease>> result = new Result<>();
        List<LearnRelease> list = learnReleaseService.queryListByIds(ids);
        result.success(true).data(list);
        return result;
    }

    /**
     * 查询学习下发
     */
    @ApiOperation(value = "查询学习下发")
    @ApiImplicitParams({
		@ApiImplicitParam(name = LearnReleaseVOMeta.ID, value = "主键", required = true, dataTypeClass = String.class),
		@ApiImplicitParam(name = LearnReleaseVOMeta.LEARN_ID, value = "名称", required = false, dataTypeClass = String.class),
		@ApiImplicitParam(name = LearnReleaseVOMeta.RCD_TIME, value = "下发时间", required = false, dataTypeClass = Date.class),
		@ApiImplicitParam(name = LearnReleaseVOMeta.UPDATE_BY, value = "修改人ID", required = false, dataTypeClass = String.class),
		@ApiImplicitParam(name = LearnReleaseVOMeta.STATUS, value = "启用状态", required = false, dataTypeClass = String.class),
		@ApiImplicitParam(name = LearnReleaseVOMeta.SDATE, value = "开始时间", required = false, dataTypeClass = Date.class, example = ""),
		@ApiImplicitParam(name = LearnReleaseVOMeta.EDATE, value = "结束时间", required = false, dataTypeClass = Date.class, example = ""),
		@ApiImplicitParam(name = LearnReleaseVOMeta.COST, value = "学习用时", required = false, dataTypeClass = Integer.class, example = "30"),
		@ApiImplicitParam(name = LearnReleaseVOMeta.CODE, value = "编号", required = false, dataTypeClass = String.class),
		@ApiImplicitParam(name = LearnReleaseVOMeta.IF_ALL_USER, value = "是否全员", required = false, dataTypeClass = String.class),
		@ApiImplicitParam(name = LearnReleaseVOMeta.SEL_ORG, value = "选择部门", required = false, dataTypeClass = String.class),
		@ApiImplicitParam(name = LearnReleaseVOMeta.RELEASE_STATUS, value = "下发状态", required = false, dataTypeClass = String.class)
	})
    @ApiOperationSupport(order = 5, author = "金杰 , maillank@qq.com", ignoreParameters = { LearnReleaseVOMeta.PAGE_INDEX, LearnReleaseVOMeta.PAGE_SIZE })
    @SentinelResource(value = LearnReleaseServiceProxy.QUERY_LIST, blockHandlerClass = { SentinelExceptionUtil.class }, blockHandler = SentinelExceptionUtil.HANDLER)
    @PostMapping(LearnReleaseServiceProxy.QUERY_LIST)
    public Result<List<LearnRelease>> queryList(LearnReleaseVO sample) {
        Result<List<LearnRelease>> result = new Result<>();
        List<LearnRelease> list = learnReleaseService.queryList(sample);
        result.success(true).data(list);
        return result;
    }

    /**
     * 分页查询学习下发
     */
    @ApiOperation(value = "分页查询学习下发")
    @ApiImplicitParams({
		@ApiImplicitParam(name = LearnReleaseVOMeta.ID, value = "主键", required = true, dataTypeClass = String.class),
		@ApiImplicitParam(name = LearnReleaseVOMeta.LEARN_ID, value = "名称", required = false, dataTypeClass = String.class),
		@ApiImplicitParam(name = LearnReleaseVOMeta.RCD_TIME, value = "下发时间", required = false, dataTypeClass = Date.class),
		@ApiImplicitParam(name = LearnReleaseVOMeta.UPDATE_BY, value = "修改人ID", required = false, dataTypeClass = String.class),
		@ApiImplicitParam(name = LearnReleaseVOMeta.STATUS, value = "启用状态", required = false, dataTypeClass = String.class),
		@ApiImplicitParam(name = LearnReleaseVOMeta.SDATE, value = "开始时间", required = false, dataTypeClass = Date.class, example = ""),
		@ApiImplicitParam(name = LearnReleaseVOMeta.EDATE, value = "结束时间", required = false, dataTypeClass = Date.class, example = ""),
		@ApiImplicitParam(name = LearnReleaseVOMeta.COST, value = "学习用时", required = false, dataTypeClass = Integer.class, example = "30"),
		@ApiImplicitParam(name = LearnReleaseVOMeta.CODE, value = "编号", required = false, dataTypeClass = String.class),
		@ApiImplicitParam(name = LearnReleaseVOMeta.IF_ALL_USER, value = "是否全员", required = false, dataTypeClass = String.class),
		@ApiImplicitParam(name = LearnReleaseVOMeta.SEL_ORG, value = "选择部门", required = false, dataTypeClass = String.class),
		@ApiImplicitParam(name = LearnReleaseVOMeta.RELEASE_STATUS, value = "下发状态", required = false, dataTypeClass = String.class)
	})
    @ApiOperationSupport(order = 8, author = "金杰 , maillank@qq.com")
    @SentinelResource(value = LearnReleaseServiceProxy.QUERY_PAGED_LIST, blockHandlerClass = { SentinelExceptionUtil.class }, blockHandler = SentinelExceptionUtil.HANDLER)
    @PostMapping(LearnReleaseServiceProxy.QUERY_PAGED_LIST)
    public Result<PagedList<LearnRelease>> queryPagedList(LearnReleaseVO sample) {
        Result<PagedList<LearnRelease>> result = new Result<>();
        PagedList<LearnRelease> list = learnReleaseService.queryPagedList(sample, sample.getPageSize(), sample.getPageIndex());
        result.success(true).data(list);
        return result;
    }
}
